//Conexao.java
//Classe para executar a conexao com os dados
package dao;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.swing.JOptionPane;

/**
 * @author Diego Heusser
 * @author Eder Schramm
 */
public class Conexao {

    private static String url;
    private static String driver;
    private static String usuario;
    private static String senha;
    
    private String motorista_url;
    private String automovel_url;
    private String nome_motorista;
    private String nome_automovel;
    private int inicio_motorista;
    private int inicio_automovel;

    public Conexao() {
        try {
            File file = new File("arquivo/configdao.txt");
            FileReader reader = new FileReader(file);
            BufferedReader buffer = new BufferedReader(reader);
            String driverConfig = buffer.readLine();
            Conexao.driver = (driverConfig.substring(7, driverConfig.length()));
            String urlConfig = buffer.readLine();
            Conexao.url = (urlConfig.substring(4, urlConfig.length()));
            String usuarioConfig = buffer.readLine();
            Conexao.usuario = (usuarioConfig.substring(8, usuarioConfig.length()));
            String senhaConfig = buffer.readLine();
            Conexao.senha = (senhaConfig.substring(6, senhaConfig.length()));
            buffer.close();
            reader.close();
            File fileAB = new File("arquivo/configarquivobinario.txt");
            FileReader readerAB = new FileReader(fileAB);
            BufferedReader bufferAB = new BufferedReader(readerAB);
            motorista_url = bufferAB.readLine().replaceAll("motorista_url=", "");
            nome_motorista = bufferAB.readLine().replaceAll("nome_motorista=", "");
            inicio_motorista =Integer.parseInt(bufferAB.readLine().replaceAll("inicio_motorista=", ""));
            automovel_url = bufferAB.readLine().replaceAll("automovel_url=", "");
            nome_automovel = bufferAB.readLine().replaceAll("nome_automovel=", "");
            inicio_automovel = Integer.parseInt(bufferAB.readLine().replaceAll("inicio_automovel=", ""));
            bufferAB.close();
            readerAB.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public Connection getConnectionJDBC() {
        try {
            Class.forName(driver);
            return DriverManager.getConnection(url, usuario, senha);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return null;
    }

    public EntityManager getConnectionJPA() {
        Map<String, Object> params = new HashMap<>();
        params.put("hibernate.connection.driver_class", driver);
        params.put("hibernate.connection.url", url);
        params.put("hibernate.connection.username", usuario);
        params.put("hibernate.connection.password", senha);
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("automovel-motoristaPU", params);
        EntityManager em = emf.createEntityManager();
        return em;
    }

    public String getMotorista_url() {
        return motorista_url;
    }

    public String getAutomovel_url() {
        return automovel_url;
    }

    public String getNome_motorista() {
        return nome_motorista;
    }

    public String getNome_automovel() {
        return nome_automovel;
    }

    public int getInicio_motorista() {
        return inicio_motorista;
    }

    public int getInicio_automovel() {
        return inicio_automovel;
    }
    
}
